﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frame.Lib.Core.Base;
using Cms.Entity.Traffic;
using Cms.DAL.Traffic;
using Frame.Lib.Web;
using Frame.Lib.Core.Company;


namespace Cms.BLL.Traffic
{
    public class PVTraficBO : CommonOperate<PVTraficInfo>
    {
        private PVTraficDAL _pv;
        public PVTraficBO()
        {
            _pv = new PVTraficDAL();
        }

        protected override BaseDAL DAL
        {
            get
            {
                return _pv;
            }
        }

        public IEnumerable<PVTraficInfo> GetList(PagerSetting setting, string startTime, string endTime,string ip)
        {
            string condition = " 1=1 ";
            if (!string.IsNullOrEmpty(startTime))
            {
                condition += " and InputTime>=@p0 ";
            }
            if (!string.IsNullOrEmpty(endTime))
            {
                condition += " and InputTime <= @p1";
            }
            if (!string.IsNullOrEmpty(ip))
            {
                condition += " and IP = @p2";
            }
            setting.Where = condition;
            IEnumerable<PVTraficInfo> list = base.GetList(setting, startTime, endTime, ip);
            return list;
        }

        public void Delete()
        {
            _pv.Delete();
        }

        public void Delete(string startTime, string endTime)
        {
            _pv.Delete(startTime, endTime);
        }

        public void Save()
        {
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            System.Web.HttpRequest request = context.Request;

            PVTraficInfo info = new PVTraficInfo();

            info.IP = request.UserHostAddress;
            info.Page = request.RawUrl;//不带域名
            if (request.UrlReferrer != null)
            {
                info.Source = request.UrlReferrer.AbsoluteUri;//带域名    
            }
            if (context.Session != null)
            {
                info.SessionID = context.Session.SessionID;
            }
            info.ID = Guid.NewGuid();
            info.InputTime = DateTime.Now;
            base.Save(info, true);

            CompanyBO company = new CompanyBO();
            CompanyInfo comInfo = company.GetInfo();

            if (comInfo != null)
            {
                comInfo.SitePVCount++;

                company.Save(comInfo);
            }
            
        }
    }
}
